//
//  SLSettingBirthdayView.m
//  BXlive
//
//  Created by sweetloser on 2020/8/27.
//  Copyright © 2020 cat. All rights reserved.
//

#import "SLSettingBirthdayView.h"
#import <PGDatePicker.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>
#import <YYCategories/YYCategories.h>


@interface SLSettingBirthdayView()<PGDatePickerDelegate>

@property(nonatomic,strong)PGDatePicker *datePicker;

@property(nonatomic,copy)NSString *birthday;

@end



@implementation SLSettingBirthdayView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame: frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __ScaleWidth(321), __ScaleWidth(330))];
    [self addSubview:self.contentView];
    self.contentView.center = CGPointMake(__kWidth/2, __kHeight/2);
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 12;
    self.contentView.backgroundColor = sl_BGColors;
    
    self.datePicker = [[PGDatePicker alloc] initWithFrame:CGRectMake(0, __ScaleWidth(15), self.contentView.mj_w, __ScaleWidth(226))];
//    [self.contentView addSubview:self.datePicker];
    self.datePicker.delegate = self;
//    self.datePicker.
    self.datePicker.datePickerMode = PGDatePickerModeDate;
    self.datePicker.showUnit = PGShowUnitTypeNone;
    self.datePicker.textFontOfSelectedRow = SLBFont(19);
    self.datePicker.textFontOfOtherRow = SLPFFont(18);
    self.datePicker.textColorOfOtherRow = sl_textSubColors;
    self.datePicker.textColorOfSelectedRow  = sl_textColors;
    self.datePicker.lineBackgroundColor = sl_divideLineColor;
    self.datePicker.minimumDate = [NSDate dateWithString:@"1919-01-01" format:@"yyyy-MM-dd"];
    self.datePicker.maximumDate = [NSDate date];
    self.datePicker.autoSelected = YES;
//    按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(28));
        make.width.mas_equalTo(__ScaleWidth(122));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.bottom.mas_equalTo(-__ScaleWidth(30));
    }];
    cancelBtn.backgroundColor = sl_subBGColors;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = __ScaleWidth(22);
    [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:BtnNormal];
    [cancelBtn sl_setTitleColor:sl_textColors];
    cancelBtn.titleLabel.font = SLBFont(14);
    [cancelBtn addTarget:self action:@selector(hiddenView) forControlEvents:BtnTouchUpInside];
    
    
    UIButton *determineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:determineBtn];
    [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(__ScaleWidth(-28));
        make.width.mas_equalTo(__ScaleWidth(122));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.bottom.mas_equalTo(-__ScaleWidth(30));
    }];
    determineBtn.backgroundColor = sl_normalColors;
    determineBtn.layer.masksToBounds = YES;
    determineBtn.layer.cornerRadius = __ScaleWidth(22);
    [determineBtn setTitle:NSLocalizedString(@"确定", nil) forState:BtnNormal];
    [determineBtn sl_setTitleColor:sl_whiteTextColors];
    determineBtn.titleLabel.font = SLBFont(14);
    [determineBtn addTarget:self action:@selector(determineBtnAction) forControlEvents:BtnTouchUpInside];
    
    
    
}
-(void)determineBtnAction{
    NSLog(NSLocalizedString(@"确定", nil));
    if (self.birthday) {
        WS(weakSelf);
        [[NewHttpRequestHuang sharedNewHttpRequestHuang] userSaveInfoWithNickName:@"" Gender:@"" Birthday:self.birthday Province_id:@"" City_id:@"" District_id:@"" Sign:@"" Fields:@"birthday" Success:^(id responseObject) {
            if ([responseObject[@"code"] intValue]==0) {
                BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
                liveUser.birthday = weakSelf.birthday;
                [BXLiveUser setCurrentBXLiveUser:liveUser];
                if (weakSelf.birthdayComplateBlock) {
                    weakSelf.birthdayComplateBlock(weakSelf.birthday);
                }
                [weakSelf hiddenView];
                
            }else{
                [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
            }
        } Failure:^(NSError *error) {
                [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"修改失败", nil)];
        }];
    }
}


- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents;{
    NSString *month = [NSString stringWithFormat:@"%ld",(long)dateComponents.month];
    if (month.length == 1) {
        month = [NSString stringWithFormat:@"0%@",month];
    }
    NSString *day = [NSString stringWithFormat:@"%ld",(long)dateComponents.day];
    if (day.length == 1) {
        day = [NSString stringWithFormat:@"0%@",day];
    }
    NSString *birthday = [NSString stringWithFormat:@"%ld-%@-%@",(long)dateComponents.year,month,day];
    
    self.birthday = birthday;
}

-(void)show{
    [super show];
    [self.contentView addSubview:self.datePicker];
}


@end
